<template>
  <div>
    <div class="up">
      <div class="head">项目新增</div>
      <!-- 点击返回跳转列表页 -->
      <div></div>
      <div>
        <router-link class="back-add" to="/ManagerHead/ProjectList/ProjectDoing">返回</router-link>
<!--      <router-link to="/ManagerHead/ProjectList">
        <button class="back">返回</button>
      </router-link>-->
      </div>
    </div>
    <div class="message-box">
      <!-- 上区域 -->
      <div class="box-1">
        <div class="head2">项目基本信息</div>
        <!-- 获取项目名称 -->
        <div class="input-box1">
          <label class="head3" for="input">项目名称：</label>
          <el-input
            class="custom-input"
            v-model="projectName"
            placeholder="【客户中文简称】+【系统功能】+【年份】"
          ></el-input>
        </div>
      </div>
      <!-- 中区域 -->
      <div class="box-2">
        <div class="block">
          <!-- 获取项目ID -->
          <div class="block-son">
            <label class="head5" for="input">项目类别：</label>
            <el-select class="down-input" v-model="projectType">
              <el-option label="内部项目" value="内部项目"></el-option>
              <el-option label="外部项目" value="外部项目"></el-option>
              <el-option label="研发项目" value="研发项目"></el-option>
            </el-select>
          </div>
          <!-- 获取重要程度 -->
          <div class="block-son">
            <label class="head5" for="input">重要程度：</label>
            <el-select class="down-input" v-model="importantLevel">
              <el-option label="非常重要" value="非常重要"></el-option>
              <el-option label="重要" value="重要"></el-option>
              <el-option label="不重要" value="不重要"></el-option>
            </el-select>
          </div>
          <!-- 获取项目名称 -->
          <div class="block-son">

          </div>
        </div>
        <div class="block">
          <!-- 获取开始时间 -->
          <div class="block-son">
            <label class="head5" for="input">开始时间：</label>
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="startDate"
              class="down-input"
            ></el-date-picker>
          </div>
          <!-- 获取结束时间 -->
          <div class="block-son">
            <label class="head5" for="input">结束时间：</label>
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="endDate"
              class="down-input"
            ></el-date-picker>
          </div>
          <div class="block-son"></div>
        </div>
        <div class="block">
          <!-- 获取预算 -->
          <div class="block-son">
            <div class="block-son">
              <label class="head5" for="input">预算：</label>
              <el-input class="down-input" v-model="budget" type="number"></el-input>
            </div>
          </div>
          <!-- 获取合同额 -->
          <div class="block-son">
            <div class="block-son">
              <label class="head5" for="input">合同额：</label>
              <el-input class="down-input" v-model="contractAmount" type="number"></el-input>
            </div>
          </div>
          <div class="block-son"></div>
        </div>
      </div>
      <!-- 下区域 -->
        <div class="box-3">
          <div class="create" @click="handleSubmit">
            <p class="head4">立即创建</p>
          </div>
        </div>
    </div>
  </div>
</template>

<script>
// 引入新增项目的接口函数
import { projectAdd } from "@/api/index";

export default {
  name: "NewlyAdd",
  data() {
    return {
      projectName: "", // 项目基本信息的属性，初始值为空
      importantLevel: "", // 项目的重要程度，单选框选择
      projectType: "", // 项目类别
      budget: "", // 预算
      contractAmount: "", // 合同额
      startDate: "", // 开始时间
      endDate: "", // 结束时间
    };
  },
  methods: {
    handleSubmit() {
      if (
          !this.projectName ||
          !this.importantLevel ||
          !this.projectType ||
          !this.budget ||
          !this.contractAmount ||
          !this.startDate ||
          !this.endDate
      ) {
        // 检查信息是否填写完整
        this.$message.warning("请输入完整信息");
      }else if (this.budget <= 0 || this.contractAmount <= 0) {
        // 检查预算和合同额是否大于0
        this.$message.warning("请输入有效的预算和合同额");
      }
      // 判断日期顺序正确
      else if (this.startDate > this.endDate) {
        this.$message.warning("开始时间不能晚于结束时间");
      } else {
        const createData = {
          projectName: this.projectName,
          importantLevel: this.importantLevel,
          projectType: this.projectType,
          budget: parseInt(this.budget),
          contractAmount: parseInt(this.contractAmount),
          startDate: new Date(this.startDate).toISOString().substring(0, 10),
          endDate: new Date(this.endDate).toISOString().substring(0, 10),
        };
        // 调用新增项目的接口函数
        projectAdd(createData)
            .then((response) => {
              // 处理后端返回的响应数据
              console.log(response);
              this.$message.success("项目创建成功");
              this.$router.push({ path: '/ManagerHead/ProjectList/ProjectDoing'});
            })
            .catch((error) => {
              // 处理请求错误
              if (
                  error.response &&
                  error.response.data.error === "项目名重复，请重新输入"
              ) {
                this.$message.warning("项目名称重复，请重新输入");
              } else {
                console.error(error);
                this.$message.error("创建项目失败，请稍后再试");
              }
            });
      }
    },
  },
};
</script>

<style scoped>
a {
  text-decoration: none; /* 移除下划线 */
  color: inherit; /* 继承父元素的颜色 */
}
.create {
  position: relative;
  background-color: rgb(2, 167, 240);
  border-radius: 5px;
  border: 2px solid rgb(121, 121, 121);
  width: 180px;
  height: 40px;
  text-align: center;
  display: flex;
}
.custom-input {
  width: 75%;
}
.down-input {
  width: 50%;
  position: relative;
  left: 20px;
}
.head5 {
  border-color: rbg(215, 215, 215);
  color: rgb(51, 51, 51);
  font-size: 18px;
  margin-right: 10px;
  text-align: right;
  position: relative;
  left: 20px;
  widows: 90px;
  height: 24px;
}
.input-box1 {
  display: flex;
  align-items: center;
  box-sizing: border-box;
  position: relative;
  top: 20px;
  left: 70px;
}
.head3 {
  border-color: rbg(215, 215, 215);
  color: rgb(51, 51, 51);
  font-size: 18px;
  margin-right: 10px;
  text-align: right;
}
.head4 {
  font-size: 18px;
  color: rgb(239, 233, 247);
  position: relative;
  top: -10px;
  left: 50px;
}
.head {
  font-family: "Arial", sans-serif; /* 设置字体 */
  font-weight: 700; /*加粗*/
  color: rgb(51, 51, 51); /* 设置颜色 */
  font-size: 28px; /* 设置字体大小 */
  box-sizing: border-box;
  margin-left: 20px;
  width: 150px;
  height: 46px;
  justify-content: space-between;
}
.head2 {
  font-family: "Arial", sans-serif; /* 设置字体 */
  font-weight: 700; /*加粗*/
  color: rgb(51, 51, 51); /* 设置颜色 */
  font-size: 20px; /* 设置字体大小 */
  position: relative; /* 设置定位 */
  top: 20px; /* 设置顶部位置 */
  left: 30px; /* 设置左侧位置 */
  box-sizing: border-box;
  width: 150px;
  height: 40px;
}
/*.background {
  display: flex;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgb(232, 232, 254);
  border: 2px solid rgb(206, 206, 207);
}*/
.back-add {
  background: aliceblue;
  border-radius: 5px;
  font-size: 24px;
  height: 80px;
  width: 50px;
  margin-right: 60px;
}
.up {
  display: flex;
  flex-direction: row;
  align-items: center;
  width: 100%;
  height: 60px;
  justify-content: space-between;
}
.message-box {
  display: flex;

  background-color: rgb(255, 255, 255);

  height: 500px;
  top: 100px;
  left: -150px;
  flex-direction: column;
}

.box-1 {
  flex: 0 0 25%;
}

.box-2 {
  flex: 0 0 50%;
  display: flex;
}

.block {
  display: flex;
  width: 33.3%;

  flex-direction: column;
}
.block-son {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.box-3 {
  flex: 0 0 25%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
